package com.voicechat.firstmet.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.content.SharedPreferencesCompat;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间：2017/06/04
 * 模块描述：SharedPreferences数据储存工具类
 */
public class SPUtils {


    private SPUtils() {
    }

    private static final String TAG = "SPUtils";
    //**************************作为一个单独的工具类**************************
    //******************************命名单独的键******************************
    public static final String SP_VERSION = "version";//当前版本号
    public static final String SP_PHONE = "phone";//当前的登陆手机号
    public static final String SP_USERSIG = "userSig";//当前登陆唯一标识Id
    public static final String SP_TOKEN = "token";//当前登陆唯一标识value
    public static final String SP_MYSELFINFO = "mySelfInfo";//当前登陆实体
    public static final String SP_SEARCH = "searchHistory";//历史搜索数据
    public static final String SP_GIF_VERSION = "gif_version";//礼物版本号
    public static final String
            SP_IS_RECEIVE_NOTIFICATION = "is_receive_notification";//是否接收通知
    //    public static final String SP_SERVICEID = "serviceId";//存储客服Id
    //************************************************************************
    private static SharedPreferences sharedPreferences = null;//xml文件
    private static final String SP_NAME = "firstmet";//xml文件名
    private static SharedPreferences.Editor editor = null;//xml内容
    public static final String SHOW_TAG = "show_tag";

    /**
     * 创建一个SharedPreferences储存机制
     *
     * @param context
     */
    public static void createSharedPreferences(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_NAME, 0);
        }
        if (editor == null) {
            editor = sharedPreferences.edit();
        }
    }

    /**
     * 提交键值对存入SharedPreferences储存机制
     *
     * @param key   键
     * @param value 值
     */
    public static void setEditorKeyValue(String key, Object value) {
        if (null != editor) {
            if (value instanceof Integer) {
                editor.putInt(key, (Integer) value);
            } else if (value instanceof Boolean) {
                editor.putBoolean(key, (Boolean) value);
            } else if (value instanceof Float) {
                editor.putFloat(key, (Float) value);
            } else if (value instanceof Long) {
                editor.putLong(key, (Long) value);
            } else {//若不能识别或者为String类型全存为String类型
                editor.putString(key, (String) value);
            }
            SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
        }
    }

    /**
     * 从SharedPreferences储存机制中取出String值
     *
     * @param key 键
     */
    public static String getEditoString(String key) {
        if (null != sharedPreferences) {
            return sharedPreferences.getString(key, "");
        }
        return null;
    }

    /**
     * 从SharedPreferences储存机制中取出int值
     *
     * @param key 键
     */
    public static int getEditorInt(String key) {
        if (null != sharedPreferences) {
            return sharedPreferences.getInt(key, -1);
        }
        return -1;
    }

    /**
     * 从SharedPreferences储存机制中取出int值
     *
     * @param key 键
     */
    public static int getEditorInt(String key, int def) {
        if (null != sharedPreferences) {
            return sharedPreferences.getInt(key, def);
        }
        return def;
    }

    /**
     * 从SharedPreferences储存机制中取出int值
     *
     * @param key 键
     */
    public static boolean getEditorBoolean(String key) {
        if (null != sharedPreferences) {
            return sharedPreferences.getBoolean(key, false);
        }
        return false;
    }

    /**
     * 从SharedPreferences储存机制中取出int值
     *
     * @param key 键
     */
    public static float getEditorFloat(String key) {
        if (null != sharedPreferences) {
            return sharedPreferences.getFloat(key, -1f);
        }
        return -1f;
    }

    /**
     * 从SharedPreferences储存机制中取出int值
     *
     * @param key 键
     */
    public static long getEditorLong(String key) {
        if (null != sharedPreferences) {
            return sharedPreferences.getLong(key, -1);
        }
        return -1;
    }

    /**
     * 清除SharedPreferences储存机制中储存的所有值
     */
    public static void clearEditorKeyValue() {
        if (null != editor) {
            editor.clear().commit();
        }
    }

    /**
     * 用于保存集合
     *
     * @param key  key
     * @param list 集合数据
     * @return 保存结果
     */
    public static <T> boolean putListData(String key, List<T> list) {
        boolean result = false;
        if (editor != null) {
            String type = list.get(0).getClass().getSimpleName();
            JsonArray array = new JsonArray();
            try {
                switch (type) {
                    case "Boolean":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Boolean) list.get(i));
                        }
                        break;
                    case "Long":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Long) list.get(i));
                        }
                        break;
                    case "Float":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Float) list.get(i));
                        }
                        break;
                    case "String":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((String) list.get(i));
                        }
                        break;
                    case "Integer":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Integer) list.get(i));
                        }
                        break;
                    default:
                        Gson gson = new Gson();
                        for (int i = 0; i < list.size(); i++) {
                            JsonElement obj = gson.toJsonTree(list.get(i));
                            array.add(obj);
                        }
                        break;
                }
                editor.putString(key, array.toString());
                result = true;
            } catch (Exception e) {
                result = false;
                e.printStackTrace();
            }
            editor.apply();
        }
            return result;
    }
    /**
     * 获取保存的List
     *
     * @param key key
     * @return 对应的Lis集合
     */
    public static <T> List<T> getListData(String key, Class<T> cls) {
        List<T> list = new ArrayList<>();
        String json = sharedPreferences.getString(key, "");
        if (!json.equals("") && json.length() > 0) {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
        }
        return list;
    }

}